package com.haoze.nurseapp.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.haoze.common.enumeration.CirculationModelEnum;
import com.haoze.common.enumeration.StateEnum;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurseapp.dto.Assessment.CommonTaskCirculationDTO;
import com.haoze.nurseapp.enumeration.OralEnum;
import com.haoze.nurseapp.service.NurseFileIndexService;
import com.haoze.nurseapp.service.OralMedicineService;
import com.haoze.nurseapp.service.PatientCirculationService;
import com.haoze.nurseapp.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.gensql.GenSql;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/nurseapp/oral")
@Api(value = "口服药接口")
public class OralMedicineController {

    @Autowired
    OralMedicineService oralMedicineService;


    @Autowired
    PatientService patientService;

    @GetMapping("/list")
    @ApiOperation(value = "口服药打印列表")
    public Result list(@RequestParam String wardDeptId, @RequestParam String freqTime, @RequestParam String date
            , @RequestParam String printFlag, @RequestParam(required = false) String keyWord, @RequestParam String inpatList,@RequestParam(defaultValue = "0") String machineSign,
                       @RequestParam(required = false) String ordersClass,@RequestParam(defaultValue = "-1") String administrationFilter) {
        return oralMedicineService.getMedicineList(wardDeptId, freqTime, date, printFlag, keyWord, inpatList,machineSign,ordersClass,administrationFilter);
    }

    @GetMapping("/timeList")
    @ApiOperation(value = "频次时间下拉选")
    public Result timeList(@RequestParam String wardDeptId, @RequestParam String freqTime, @RequestParam String date
            , @RequestParam String printFlag, @RequestParam(required = false) String keyWord, @RequestParam String inpatList,@RequestParam(defaultValue = "0") String machineSign, @RequestParam(required = false) String ordersClass,@RequestParam(required = false) String administrationFilter) {
        return oralMedicineService.getTimeList(wardDeptId, freqTime, date, printFlag, keyWord, inpatList,machineSign,ordersClass,administrationFilter);
    }

//    @GetMapping("/patlist")
//    @ApiOperation(value = "病人列表")
//    public Result patlist(@RequestParam(required = false) String wardId,@RequestParam(required = false,defaultValue = "1")Integer page,
//                       @RequestParam(required = false,defaultValue = "50")Integer pageSzie, @RequestParam(required = false) String keyWord) {
//        return oralMedicineService.getpatList(wardId,page,pageSzie,keyWord);
//    }

    @PostMapping("/print")
    @ApiOperation(value = "口服药打印列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "模型名称", dataType = "String", paramType = "modelName")
    })
    public Result print(@RequestBody List<Map<String,Object>> data) {
        return oralMedicineService.prints(data);
    }

    @GetMapping("/scan")
    @ApiOperation(value = "移动端扫描")
    public Result scan(@RequestParam String oralCode,@RequestParam String type) {
        //二维码数据查询到需要的医嘱信息
        //需要另外查询是否有需要踢掉的数据,如取消的医嘱
        return oralMedicineService.scan(oralCode,type,"1",null);//0否 1是 是否生成任务
    }

    @GetMapping("/scanNew")
    @ApiOperation(value = "口服药扫描")
    public Result scanNew(@RequestParam(required = false) String oralCode,@RequestParam String type,@RequestParam(required = false) String inpatNum
            ,@RequestParam(required = false) String date,@RequestParam(required = false,defaultValue = "1") String generateFlag,
                          @RequestParam(required = true) String wardDeptId,@RequestParam(required = false) String uuid) {
        //二维码数据查询到需要的医嘱信息
        //需要另外查询是否有需要踢掉的数据,如取消的医嘱
        return oralMedicineService.scanNew(oralCode,type,inpatNum,date,generateFlag,wardDeptId,uuid);
    }

    @GetMapping("/oralList")
    @ApiOperation(value = "口服药任务节点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区ID", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "查询类型 dispensing待摆药 dose待给药 take待服药 recovery已回收 termination终止 finish完成", dataType = "String"),
            @ApiImplicitParam(name = "date", value = "时间", dataType = "String"),
            @ApiImplicitParam(name = "freqTime", value = "频次时间", dataType = "String"),
            @ApiImplicitParam(name = "keyWord", value = "病区ID", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "分页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer")
    })
    public Result oralList(@RequestParam(required = false) String wardId,@RequestParam(required = false) String type,@RequestParam(required = false) String date,
                           @RequestParam(required = false) String freqTime,@RequestParam(required = false) String keyWord,@RequestParam(required = false,defaultValue = "1") Integer page
            ,@RequestParam(required = false,defaultValue = "20") Integer pageSize) {
        //摆药查询 : 需要查询医嘱列表 无节点和0状态任务节点的数据
        //待摆药,待给药,已服药 查询关系表,会维护好
        if(StateEnum.BY_TYPE.getValue().equals(type)){//摆药
            return oralMedicineService.getByList(wardId,null,date,keyWord,page,pageSize,OralEnum.BY_NODE.getName(),freqTime);
        }else if(StateEnum.GY_TYPE.getValue().equals(type)){//给药
            return oralMedicineService.getRelationList(wardId,null,date,keyWord,page,pageSize, OralEnum.GY_NODE.getName(),null,null,freqTime);
        }else if(StateEnum.FY_TYPE.getValue().equals(type)){//服药
            return oralMedicineService.getRelationList(wardId,null,date,keyWord,page,pageSize,OralEnum.FY_NODE.getName(),null,null,freqTime);
        }else if(StateEnum.HS_TYPE.getValue().equals(type)){//回收
            return oralMedicineService.getRelationList(wardId,null,date,keyWord,page,pageSize,OralEnum.GY_NODE.getName(),OralEnum.GY_NODE.getName(),null,null);
        }else if(StateEnum.ZZ_TYPE.getValue().equals(type)){//终止
            return oralMedicineService.getReturnList(wardId,date,keyWord,page,pageSize);//OralEnum.ZZ_NODE.getName()
        }else if(StateEnum.WC_TYPE.getValue().equals(type)){//已服药
            return oralMedicineService.getRelationList(wardId,null,date,keyWord,page,pageSize,OralEnum.WC_NODE.getName(),null,null,null);
        }
        return ResultGenerator.genOkResult();
    }

    @GetMapping("/oralTimeList")
    @ApiOperation(value = "口服药任务节点时间列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区ID", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "查询类型 dispensing待摆药 dose待给药 take待服药 recovery已回收 termination终止 ", dataType = "String"),
            @ApiImplicitParam(name = "date", value = "时间", dataType = "String"),
            @ApiImplicitParam(name = "keyWord", value = "病区ID", dataType = "String")
    })
    public Result oralTimeList(@RequestParam(required = false) String wardId,@RequestParam(required = false) String type,@RequestParam(required = false) String date,
                          @RequestParam(required = false) String keyWord) {
        if(StateEnum.BY_TYPE.getValue().equals(type)){//摆药
            return oralMedicineService.getAppTimeList(oralMedicineService.getByList(wardId,null,date,keyWord,0,0,OralEnum.BY_NODE.getName(),null));
        }else if(StateEnum.GY_TYPE.getValue().equals(type)){//给药
            return oralMedicineService.getAppTimeList(oralMedicineService.getRelationList(wardId,null,date,keyWord,0,0, OralEnum.GY_NODE.getName(),null,null,null));
        }else if(StateEnum.FY_TYPE.getValue().equals(type)){//服药
            return oralMedicineService.getAppTimeList(oralMedicineService.getRelationList(wardId,null,date,keyWord,0,0,OralEnum.FY_NODE.getName(),null,null,null));
        }else if(StateEnum.HS_TYPE.getValue().equals(type)){//回收
            return oralMedicineService.getAppTimeList(oralMedicineService.getRelationList(wardId,null,date,keyWord,0,0,OralEnum.GY_NODE.getName(),OralEnum.GY_NODE.getName(),null,null));
        }else if(StateEnum.ZZ_TYPE.getValue().equals(type)){//终止
            return oralMedicineService.getAppTimeList(oralMedicineService.getRelationList(wardId,null,date,keyWord,0,0,OralEnum.ZZ_NODE.getName(),OralEnum.ZZ_NODE.getName(),null,null));
        }
        return ResultGenerator.genOkResult();
    }

    @GetMapping("/oralNumList")
    @ApiOperation(value = "口服药任务节点列表任务数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardId", value = "病区ID", dataType = "String"),
            @ApiImplicitParam(name = "date", value = "时间", dataType = "String")
    })
    public Result oralNumList(@RequestParam(required = false) String wardId,@RequestParam(required = false) String date,
                           @RequestParam(required = false) String freqTime,@RequestParam(required = false) String keyWord,@RequestParam(required = false,defaultValue = "1") Integer page
            ,@RequestParam(required = false,defaultValue = "20") Integer pageSize) {
        SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(StringUtils.isBlank(date)){
            date = "";
        }
        String byDate = "";
        if(StringUtils.isBlank(date)){
            byDate = sdf.format(new Date());
        }else{
            byDate = date;
        }
        Map result = new HashMap();

        //摆药
        Result result1 = oralMedicineService.getByList(wardId,null,byDate,null,0,0,OralEnum.BY_NODE.getName(),freqTime);
        Map<String,String> pageInfo1 = JSON.parseObject(JSON.toJSONString(result1.getData()), Map.class);
        result.put("dispensing",pageInfo1.get("total"));
        //给药
        Result result2 = oralMedicineService.getRelationList(wardId,null,date,null,0,0, OralEnum.GY_NODE.getName(),null,null,freqTime);
        Map<String,String> pageInfo2 = JSON.parseObject(JSON.toJSONString(result2.getData()), Map.class);
        result.put("dose",pageInfo2.get("total"));
        //服药
        Result result3 = oralMedicineService.getRelationList(wardId,null,date,null,0,0,OralEnum.FY_NODE.getName(),null,null,freqTime);
        Map<String,String> pageInfo3 = JSON.parseObject(JSON.toJSONString(result3.getData()), Map.class);
        result.put("take",pageInfo3.get("total"));
        //回收
        Result result4 = oralMedicineService.getRelationList(wardId,null,null,null,0,0,OralEnum.GY_NODE.getName(),StateEnum.HS_TYPE.getValue(),null,null);
        Map<String,String> pageInfo4 = JSON.parseObject(JSON.toJSONString(result4.getData()), Map.class);
        result.put("recovery",pageInfo4.get("total"));
        //终止
        Result result5 = oralMedicineService.getReturnList(wardId,null,keyWord,page,pageSize);//OralEnum.ZZ_NODE.getName()
        Map<String,String> pageInfo5 = JSON.parseObject(JSON.toJSONString(result5.getData()), Map.class);
        result.put("termination",pageInfo5.get("total"));
        //已完成
        Result result6 = oralMedicineService.getRelationList(wardId,null,date,null,0,0,OralEnum.WC_NODE.getName(), null,null,null);//OralEnum.ZZ_NODE.getName()
        Map<String,String> pageInfo6 = JSON.parseObject(JSON.toJSONString(result6.getData()), Map.class);
        result.put("finish",pageInfo6.get("total"));

        return ResultGenerator.genOkResult(result);
    }

    @GetMapping("/check")
    @ApiOperation(value = "核对进入下一个节点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "核对类型 turnBack 药物回归 confirm核对下一个节点 termination终止任务", dataType = "String")
    })
    public Result check(@RequestParam(required = false)String wardId, @RequestParam(required = false)String oralCode,
                        @RequestParam(required = false)List<String> taskQrCodes,@RequestParam(required = false) String person,
            @RequestParam(required = false) String type,@RequestParam(required = false) String reason) {
        //type turnBack  confirm
        if(StateEnum.TB_TYPE.getValue().equals(type)){//药物回收
            return oralMedicineService.turnBack(wardId,oralCode,taskQrCodes,person,reason,StateEnum.TB_TYPE.getValue());
        }else if(StateEnum.HD_TYPE.getValue().equals(type)){//核对进入下一个节点
            return oralMedicineService.check(wardId,oralCode,taskQrCodes,person);
        }else if(StateEnum.ZZ_TYPE.getValue().equals(type)){//终止
            return oralMedicineService.turnBack(wardId,oralCode,taskQrCodes,person,reason,StateEnum.ZZ_TYPE.getValue());
        }
        return ResultGenerator.genOkResult();
    }


}